using System;
using ColorBox4Sitecore.Colorbox;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;

namespace ColorBox4Sitecore.Helpers
{
    public static class ColorboxTransitionHelper
    {
        public static ColorboxTransition GetColorboxTransitionValueFromField(this Item item, string fieldName)
        {
            return item.GetColorboxTransitionValueFromField(fieldName, ColorboxTransition.None);
        }

        public static ColorboxTransition GetColorboxTransitionValueFromField(this Item item, string fieldName, ColorboxTransition defaultValue)
        {
            if (item == null)
            {
                Log.Error("ColorboxUnitHelper.GetColorboxUnitValueFromField(): item is null", typeof(ColorboxTransitionHelper));
                throw new ArgumentNullException("item");
            }

            var fld = (ReferenceField)item.Fields[fieldName];


            if (fld == null || fld.TargetItem == null)
                return defaultValue;

            switch (fld.TargetItem.Name)
            {
                case "None":
                    return ColorboxTransition.None;
                case "Elastic":
                    return ColorboxTransition.Elastic;
                case "Fade":
                    return ColorboxTransition.Fade;
                default:
                    return defaultValue;
            }
        }

        public static string ToJsString(ColorboxTransition unit)
        {
            switch (unit)
            {
                case ColorboxTransition.None:
                    return "none";
                case ColorboxTransition.Elastic:
                    return "elastic";
                case ColorboxTransition.Fade:
                    return "fade";
                default:
                    return string.Empty;
            }
        }
    }
}